﻿using ch07.binary.module;
using ch07.binary.type;

namespace ch07.binary
{
    using MemType = Limits;

    public struct Module
    {
        internal uint Magic; //魔数
        internal uint Version; // 版本号
        internal CustomSec[] CustomSecs; //自定义段
        internal FuncType[] TypeSec; //
        internal Import[] ImportSec; //导入段
        internal uint[] FuncSec; //函数段
        internal TableType[] TableSec; //表段
        internal MemType[] MemSec; //内存段
        internal Global[] GlobalSec; //全局段
        internal Export[] ExportSec; //导出段
        internal uint StartSec; //起始段
        internal Elem[] ElemSec; //元素段
        internal Code[] CodeSec; //代码段
        internal Data[] DataSec; //数据段

        internal FuncType GetBlockType(BlockType bt)
        {
            return bt switch
            {
                BlockType.BlockTypeI32 => new FuncType {ResultTypes = new[] {ValType.ValTypeI32}},
                BlockType.BlockTypeI64 => new FuncType {ResultTypes = new[] {ValType.ValTypeI64}},
                BlockType.BlockTypeF32 => new FuncType {ResultTypes = new[] {ValType.ValTypeF32}},
                BlockType.BlockTypeF64 => new FuncType {ResultTypes = new[] {ValType.ValTypeF64}},
                BlockType.BlockTypeEmpty => new FuncType(),
                _ => TypeSec[(int) bt]
            };
        }
    }
}